package dbPhase.hypeerweb;

import org.junit.Test;

import phase2Specification.testing.hypeerweb.ExpectedResult;

import junit.framework.TestCase;


// To use this class, comment out deletion code and comment in print statement in Node's removeFromHyPeerWeb() method
public class FindDeletionPointTest  extends TestCase {
    private static final int PHASE_2_TEST_COUNT = 10912;
    private static final int HYPEERWEB_SIZE = 32;
    private static int errorCount = 0;
    private static boolean VERBOSE = true;

	public static HyPeerWeb hypeerweb = HyPeerWeb.getSingleton();
	@Test
	public void testAddNode() {
		hypeerweb.clear();
		boolean error = false;

//		Node node0 = new Node(0);
//		hypeerweb.addToHyPeerWeb(node0, null);
//		Node node1 = new Node(0);
//		hypeerweb.addToHyPeerWeb(node1, node0);
//
//		System.err.println("Size is empty, added node0");
//
//		if (node0.webId.getValue() != 0) {
//			System.err.println("Node 0: Webid Wrong: " + node0.webId.getValue()); error = true; errorCount++;}
//
//
//		assertFalse(error);

		System.out.println("\nTesting Phase 2");
        hypeerweb.clear();
        errorCount = 0;

        boolean insertionError = false;
        System.out.print("    ");

        for (int size = 1; size <= HYPEERWEB_SIZE; size++) {
            if (VERBOSE) {
                System.out.println("Testing FindDP on HyPeerWeb of size " + size + "/" + HYPEERWEB_SIZE);
            }
            else {
                System.out.print(size + " ");
            }

//            hypeerweb.clear();
//            Node node0 = new Node(0);
//            hypeerweb.addToHyPeerWeb(node0, null);
//            Node firstNode = hypeerweb.getNode(0);
//            SimplifiedNodeDomain simplifiedNodeDomain = firstNode.constructSimplifiedNodeDomain();
//            ExpectedResult expectedResult = new ExpectedResult(1, 0);
//
//            if (!simplifiedNodeDomain.equals(expectedResult)) {
//                insertionError = true;
//                printErrorMessage(size, null, simplifiedNodeDomain, expectedResult);
//            }

            for (int startNodeId = 0; startNodeId < size - 1; startNodeId++) {
                createHyPeerWebWith(size - 1);
                //Hypeerweb is created!
                System.out.println("Calling removeFromHyPeerWeb on node " + startNodeId + ", size of hypeerweb is " + hypeerweb.size() + ".  ");
              //  hypeerweb.removeNode(hypeerweb.getNode(startNodeId));
                hypeerweb.getNode(startNodeId).removeFromHyPeerWeb();

//                Node node = new Node(0);
//                Node startNode = hypeerweb.getNode(startNodeId);
//                hypeerweb.addToHyPeerWeb(node, startNode);

//                for (int i = 0; i < size; i++) {
//                    Node nodei = hypeerweb.getNode(i);
//                    simplifiedNodeDomain = nodei.constructSimplifiedNodeDomain();
//                    expectedResult = new ExpectedResult(size, i);
//
//                    if (!simplifiedNodeDomain.equals(expectedResult)) {
//                        insertionError = true;
//                        printErrorMessage(size, startNode, simplifiedNodeDomain, expectedResult);
//                    }
//                }
            }
        }
        if (!VERBOSE) {
            System.out.println();
        }

        if (insertionError) {
            double decimalPercent = ((double)PHASE_2_TEST_COUNT - (double) errorCount) / PHASE_2_TEST_COUNT;
            int integerPercent = (int) (decimalPercent * 100.0d);
            System.out.println("    Insertion Error: FindDP Score = " + integerPercent + "%");
        }

        else {
            System.out.println("    No Insertion Errors: FindDP 2 Score = 100%");
        }
	}
	private void printoutstates() {
		System.err.println("webSize: " + hypeerweb.size());
		for (int a = 0; a < hypeerweb.size(); a++) {
			Node h = hypeerweb.getNode(a);
			System.err.println(h.getWebId().getValue() + " is " + h.nodeState);
		}

	}

    private static void printErrorMessage(int size, Node startNode, SimplifiedNodeDomain simplifiedNodeDomain, ExpectedResult expectedResult) {
        errorCount++;

        System.out.println("-------------------------------------------------------------");
        System.out.println("INSERTION ERROR when HyPeerWebSize = " + size + ", Start Location: " + startNode);
        System.out.println("Node Information:");
        System.out.println(simplifiedNodeDomain);
        System.out.println();
        System.out.println("Expected Information");
        System.out.println(expectedResult);
    }

	 private static void createHyPeerWebWith(int numberOfNodes) {
	        hypeerweb.clear();
	        Node node0 = new Node(0);
	        hypeerweb.addToHyPeerWeb(node0, null);

	        for (int i = 1; i < numberOfNodes; i++) {
	            Node node = new Node(0);
	            node0.addToHyPeerWeb(node);
	        }
	    }
}
